<?php

header('content-type:text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

//先屏蔽掉php打印出来的错误信息
error_reporting(E_ALL || ~E_NOTICE);
require_once '../utils/response.php';
require_once  '../utils/db.php';

//接收PUT过来的账号和密码
$username = json_decode(file_get_contents("php://input"))->username;
$password = json_decode(file_get_contents("php://input"))->password;

//连接数据库
try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403, '数据库连接失败', $e->getMessage());
}

//预处理语句进行登录名和密码的验证
$sql = "SELECT * FROM user WHERE zwh_name=? && zwh_pwd =?";

$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ss', $username, $password);

// $data = array(
//     'username' => $username,
//     'password' => $password,
// );

//执行sql语句
if ($mysqli_stmt->execute()) {

    if ($mysqli_stmt->fetch()) {
        return Response::show('200', '登录成功');
    } else {
        return Response::show('400', '登录失败,用户名或密码错误');
    }
} else {
    return Response::show('404', '登录失败,服务器没响应');
}
